<?php
$theme_options = get_option('po_theme');
$qty = $theme_options['additional_sidebars'];
	// DEFAULT SIDEBAR
	if ( function_exists('register_sidebar') )
	register_sidebars(1,array('name' => 'Default Sidebar',
		'before_widget' => '<li>',
		'after_widget' => '</li>',
		'before_title' => '<h3>',
		'after_title' => '</h3>'
	));
	// FOOTER SIDEBARS
	if ( function_exists('register_sidebar') )
	register_sidebars(3,array('name' => 'Footer Sidebar %d',
		'before_widget' => '<li>',
		'after_widget' => '</li>',
		'before_title' => '<h3>',
		'after_title' => '</h3>'
	));
	// ADDTIONAL SIDEBARS
	if ( function_exists('register_sidebar') )
	register_sidebars($qty,array('name' => 'Side %d',
		'before_widget' => '<li>',
		'after_widget' => '</li>',
		'before_title' => '<h3>',
		'after_title' => '</h3>'
	));
	// PORTFOLIO SIDEBAR
	if ( function_exists('register_sidebar') )
	register_sidebars(1,array('name' => 'Portfolio Sidebar',
		'before_widget' => '<li>',
		'after_widget' => '</li>',
		'before_title' => '<h3>',
		'after_title' => '</h3>'
	));
// START CUSTOM WRITE POST PANEL
$new_meta_boxes = 
array(
"full_image" => array(
	"name" => "full_image",
	"type" => "input",
	"std" => "",
	"title" => "Post Image",
	"description" => "Paste the image's URL here after you have uploaded it. The script will create a thumbnail automatically. The image can be an arbitrary resolution, but no more than 2&nbsp;MB.",
	),
"sidebar" => array(
	"name" => "sidebar",
	"type" => "select",
	"std" => "",
	"title" => "Custom Sidebar",
	"description" => "Select the sidebar. If you'd like you can set an unique sidebar for each post/page.",
	)
);

function new_meta_boxes() {
// ADD CSS STYLE
echo '<style type="text/css">
.pandameta {			width:100%; }
.pandameta .tda {		width:120px; vertical-align:top; }
.pandameta .tda p {		white-space:nowrap; font-weight:bold; margin:11px 0 0 0 !important; }
.pandameta p span {		display:block; padding:0 0 0 6px !important; }
</style>';
// FOR EACH LOOP
global $post, $new_meta_boxes, $qty;
	foreach($new_meta_boxes as $meta_box) {
		echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
		if( $meta_box['type'] == "input" ) { 
			$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
			if($meta_box_value == "")
				$meta_box_value = $meta_box['std'];
				// POST IMAGE
				echo'
				<table class="pandameta">
					<tr>
						<td class="tda"><p>'.$meta_box['title'].'</p></td>
						<td><p><input style="width:99%" type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="" /><span>'.$meta_box['description'].'</span></p></td>
					</tr>
				</table>
				';
		} elseif( $meta_box['type'] == "select" ) {
			$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
			if($meta_box_value == "")
				$meta_box_value = $meta_box['std'];
				// SELECT THE SIDEBAR
				?>
				<table class="pandameta">
					<tr>
						<td class="tda"><p><?php echo $meta_box['title']; ?></p></td>
						<td>
							<p>
								<select name="<?php echo $meta_box['name']; ?>_value" id="<?php echo $meta_box['name']; ?>_value">
									<option value="Default Sidebar"<?php if ($meta_box_value == "Default Sidebar") {echo 'selected';} ?>>Default Sidebar</option>
									<?php for ($s=1; $s<($qty+1); $s++) { ?>
										<option value="Side <?php echo $s ?>"<?php if ($meta_box_value == "Side ".$s) {echo 'selected';} ?>>Side <?php echo $s ?></option>
									<?php }; ?>
								</select>
								<span><?php echo $meta_box['description']; ?></span>
							</p>
						</td>
					</tr>
				</table>
				<?php ;
		}
	}
}

// REGISTER WRITE PANEL
function create_meta_box() {
global $theme_name, $new_meta_boxes;
	if (function_exists('add_meta_box') ) {
	add_meta_box( 'new-meta-boxes', 'Custom Write Panel', 'new_meta_boxes', 'post', 'normal', 'high' );
	add_meta_box( 'new-meta-boxes', 'Custom Write Panel', 'new_meta_boxes', 'page', 'normal', 'high' );
	}
}

function save_postdata( $post_id ) {
	global $post, $new_meta_boxes;  
		foreach($new_meta_boxes as $meta_box) {  
		
		// VERIFY 
		if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {  
		return $post_id;  
		}  
	
	if ( 'page' == $_POST['post_type'] ) {  
	if ( !current_user_can( 'edit_page', $post_id ))  
	return $post_id;  
	} else {  
	if ( !current_user_can( 'edit_post', $post_id ))  
	return $post_id;  
	}  
	
	$data = $_POST[$meta_box['name'].'_value'];
	
	if(get_post_meta($post_id, $meta_box['name'].'_value') == "")  
	add_post_meta($post_id, $meta_box['name'].'_value', $data, true);  
	elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))  
	update_post_meta($post_id, $meta_box['name'].'_value', $data);  
	elseif($data == "")  
	delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));  
	}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
?>